﻿using System;
using System.Collections.Generic;
using System.Linq;
using DistributeTransaction.DAL;
using DistributeTransaction.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;

namespace DistributeTransaction.Tests
{
    /// <summary>
    ///This is a test class for CardRepositoryTest and is intended
    ///to contain all CardRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CardRepositoryTest
    {
        private int CardId;

        /// <summary>
        ///A test for CardRepository Constructor
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void CardRepositoryConstructorTest()
        {
            DatabaseContext context = new DatabaseContext();
            CardRepository target = new CardRepository(context);
        }

        /// <summary>
        ///A test for CreatNewItem
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void CreatNewItemTest()
        {
            DatabaseContext context = new DatabaseContext();
            CardRepository target = new CardRepository(context);
            CardInfo cardInfo = new CardInfo();
            cardInfo.CardNumber = "4111111111111111";
            cardInfo.CardType = "visa";
            cardInfo.ExpireMonth = "12";
            cardInfo.ExpireYear = "12";
            cardInfo.HoldName = "test first name & last name";
            cardInfo.ValidCode = "345";

            Guid customerId = new Guid("68F9761A-857C-4D0B-A53B-685B37672739"); 
            bool expected = true; 
            bool actual;
            actual = target.CreatNewItem(cardInfo, customerId);
            CardId = cardInfo.AccountId;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for DeleteCardById
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void DeleteCardByIdTest()
        {
            DatabaseContext context = new DatabaseContext();
            CardRepository target = new CardRepository(context);
            int Id = CardId; 
            target.DeleteCardById(Id);
            CreatNewItemTest();
        }

        /// <summary>
        ///A test for FindCardById
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void FindCardByIdTest()
        {
            DatabaseContext context = new DatabaseContext();
            CardRepository target = new CardRepository(context);
            if (CardId == 0)
            {
                SaveCardInfoTest();
            }
            CardInfo actual = target.FindCardById(CardId);
            Assert.IsNotNull(actual);
            Assert.AreEqual("4111111111111111", actual.CardNumber.TrimEnd());
        }

        /// <summary>
        ///A test for GetCardInfos
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void GetCardInfosTest()
        {
            DatabaseContext context = new DatabaseContext();
            CardRepository target = new CardRepository(context);
            Guid userId = new Guid("68F9761A-857C-4D0B-A53B-685B37672739"); 
            IEnumerable<CardInfo>  actual = target.GetCardInfos(userId);
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Count() >= 1);
        }

        /// <summary>
        ///A test for SaveCardInfo
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void SaveCardInfoTest()
        {
            DatabaseContext context = new DatabaseContext();
            CardRepository target = new CardRepository(context);
            if (CardId == 0)
            {
                CreatNewItemTest();
            }
            CardInfo cardInfo = target.FindCardById(CardId);
            cardInfo.HoldName = "test first name - last name";
            bool actual= target.SaveCardInfo(cardInfo);
            Assert.AreEqual(true, actual);
        }
    }
}
